<template>
	<view class="container">
		<view class="list">
			<view class="cell-box" v-if="list.length == 0">
				<view class="title">暂无年审认证安排</view>
				<!-- <view class="button" @click="go">去认证</view> -->
			</view>
			<view class="cell-box" v-for="item in list">
				<view class="icon-wrap" v-if="item.activeStatus=='未开始'">
					<image class="icon" src="../../static/img/icons/wait.png" mode="widthFix"></image>
					<view class="icon-label">{{item.activeStatus}}</view>
				</view>
				<view class="icon-wrap" v-else-if="item.activeStatus=='已开始'" @click="open(item)">
					<image class="icon" src="../../static/img/icons/ok.png" mode="widthFix"></image>
					<view class="icon-label">{{item.activeStatus}}</view>
				</view>
				<view class="icon-wrap" v-else-if="item.activeStatus=='已结束'">
					<image class="icon" src="../../static/img/icons/end.png" mode="widthFix"></image>
					<view class="icon-label">{{item.activeStatus}}</view>
				</view>
				<view class="icon-wrap" v-else>
					<image class="icon" src="../../static/img/icons/question.png" mode="widthFix"></image>
					<view class="icon-label">{{item.activeStatus}}</view>
				</view>
				<view class="text-wrap" @click="open(item)">
					<view class="text year">{{item.title}}</view>
					<view class="text">年份时段：{{item.year}}年 {{item.period}}</view>
					<view class="text">时间日期：{{item.startDate?item.startDate:''}} - {{item.endDate?item.endDate:''}}</view>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import Store from '@/store/index.js';
	import { ApiIdentifySchedule, ApiIdentifySchedulePerson } from "@/api/identify/index.js";
	import moment from "moment";
	import 'moment/locale/zh-cn';
	
	export default {
		data() {
			return {
				operation: null, 	//操作类型：认证、查询
				user:{},
				list: [],
				isDisabled: false   //防止多次点击
			}
		},
		created() {
			moment.locale('zh-cn');
			this.user = Store.getUser();
			this.getList();
		},
		onLoad(option) {
			this.operation = option.operation;
		},
		onShow() {
			this.isDisabled = false;
		},
		methods: {
			getList(){
				if(this.user.type=='people'){
					//扶助对象：查询扶助对象的年审安排
					ApiIdentifySchedule.querySchedulesByPerson(this.user.id).then(res=>{
						let today = moment().unix();
						res.data.forEach(item=>{
							item.activeStatus = this.getActiveStatus(item, today);
						})
						this.list = res.data;
					})
				} else {
					//服务人员：查询服务人员的年审安排
					ApiIdentifySchedule.querySchedulesByStaff(this.user.id).then(res=>{
						let today = moment().unix();
						res.data.forEach(item=>{
							item.activeStatus = this.getActiveStatus(item, today);
						})
						this.list = res.data;
					})
				}
			},
			
			getActiveStatus(schedule, today){
				if(!schedule.startDate || schedule.startDate == '')return '未知';
				if(!schedule.endDate || schedule.endDate == '')return '未知';
				if(today < moment(schedule.startDate, 'YYYY-MM-DD').unix()){
					return '未开始';
				} else if(today>=moment(schedule.startDate, 'YYYY-MM-DD').unix() && today<=moment(schedule.endDate, 'YYYY-MM-DD').unix()){
					return '已开始';
				} else if(today > moment(schedule.endDate, 'YYYY-MM-DD').unix()){
					return '已结束';
				}
				return '未知';
			},
			
			open(schedule){
				if (this.isDisabled) return;
				this.isDisabled = true;
				let that = this;
				if(this.operation == 'identify'){
					//认证：
					if(!schedule.activeStatus=='已开始'){
						uni.showToast({
							title: '认证'+schedule.activeStatus
						})
						return;
					}
					if(this.user.type == 'people'){
						//奖扶对象自己操作
						//认证服务选择
						let config = this.$store.getAreaConfig();
						if(!config || !config.recognitionProvider || config.recognitionProvider=='' || config.recognitionProvider == 'weixin'){
							//微信认证：先检查是否认证过 
							that.getSchedulePerson(schedule, that.user.id);
						} else {
							uni.navigateTo({
								url: '/pages/identify/index?scheduleId='+schedule.id
							})
						}
					} else {
						uni.navigateTo({
							url: '/pages/identify/person-list?scheduleId='+schedule.id
						})
					}
				} else {
					//查询认证结果
					if(schedule.activeStatus=='未开始' || schedule.activeStatus=='未知'){
						uni.showToast({
							title: '认证'+schedule.activeStatus
						})
						return;
					}
					if(this.user.type == 'people'){
						uni.navigateTo({
							url: '/pages/identify/search?scheduleId='+schedule.id
						})
					} else {
						uni.navigateTo({
							url: '/pages/identify/search-person-list?scheduleId='+schedule.id
						})
					}
				}
			},
			
			//查询按排名单认证记录
			getSchedulePerson(schedule, personId){
				ApiIdentifySchedulePerson.list({
					scheduleId: schedule.id,
					personId: personId
				}).then(res=>{
					let schedulePerson = res.data[0];
					let operable = true;
					res.data.forEach(item=>{
						//是否可操作：认证成功或审批通过（不可操作），其它可（重复）操作
						if(item.identifyResult == '0' || item.checkStatus=='通过'){
							operable = false;
						} else {
							operable = true;
						}
					})
					//最多提交次数
					let config = this.$store.getAreaConfig();
					if(res.data[0].submitTimes>config.maxRecognitionTimes){
						operable = false;
						uni.showModal({
							title: '超限提醒',
							content: '您已提交多次认证，请等待审核即可。'
						})
					}
					
					if(operable){
						this.wxRecognition(schedule);
					} else {
						uni.showModal({
							title:'提示',
							content: '您已认证，请在首页认证查询中查看审核结果'
						})
						this.isDisabled = false;
					}
				})
			},
			
			//打开微信人脸认证
			wxRecognition(schedule){
				let that = this;
				wx.startFacialRecognitionVerify({
					name: this.user.name,
					idCardNumber: this.user.idNumber,
					complete(res){
						// console.log(3333);
						// console.log(res);
						if(res.errCode === 0){
							let data = {
								verifyResult: res.verifyResult,
								personId: that.user.id,
								scheduleId: schedule.id										
							};
							ApiIdentifySchedulePerson.identifyVerify(data).then(response=>{
								if(response.code == 200){
									uni.showModal({
										title:'认证结果',
										content: '认证通过',
										complete() {
											uni.navigateBack();
										}
									})
								}
							});
						} else {
							uni.showToast({
								title: res.errMsg
							})
						}
					},
				});
			}
		}
	}
</script>

<style>
	.container {
		width: 750upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 0;
		margin: 0;
	}
	.list{
		width: 750upx;
		padding: 30upx;
		box-sizing: border-box;
	}
	.list .cell-box{
		background-color: white;
		border-radius: 5upx;
		padding: 40upx;
		display: flex;
		margin-bottom: 30upx;
		align-items: center;
	}
	.list .cell-box .icon-wrap{
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.list .cell-box .icon{
		width: 100upx;
		height: 100upx;
	}
	.list .cell-box .icon-wrap .icon-label{
		margin-top: 10upx;
		font-size: 14px;
		font-weight: bold;
	}
	.list .cell-box .text-wrap{
		margin-left: 30upx;
	}
	.list .cell-box .text-wrap .text{
		margin-bottom: 15upx;
	}
	.list .cell-box .text-wrap .year{
		font-size: 18px;
		font-weight: bold;
	}
	.list .cell-box .title {
		font-size: 14px;
		font-weight: bold;
	}
	.list .cell-box .button {
		padding: 15upx 30upx;
		margin-left: 40upx;
		border-radius: 40upx;
		color: white;
		background: linear-gradient(135deg, #FFA933, #FFE169 );
	}
</style>
